home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC-SIG: World of Games
/
PC-SIG World of Games (CDRM1080710) (1993).iso
/
ENT
/
DISK2468.ZIP
/
MM
/
MM.C
next >
Wrap
Text File
|
1990-05-23
|
53KB
|
1,746 lines
/* Mastermind, Mini Mastermind & Advanced Mastermind */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
#include <conio.h>
#include <ctype.h>
#include "\tc\myincs\mouse.h" /* mouse routines */
#include "\tc\myincs\myscreen.h" /* cursor & border control routines */
#include "\tc\myincs\popup.h" /* popup menu routines */
#define TRUE 1
#define FALSE 0
#define RTN 0x0d
#define ESC 0x1b
#define F1 59
#define NUL 0x00
#define LEFT 0
#define RIGHT 1
char ch, InKey;
int FunctionKey;
int RestartFlag;
int Help;
int Done;
int mode; /* 0:Mastermind; 1:Mini Mastermind; 2:Advanced Mastermind */
int MaxTurns; /* is dependent upon mode */
int GoForever;
int Forever;
int LeftButton;
int RightButton;
int ShowMainMenu;
int HasCheated = FALSE;
int UseKbd = FALSE;
int MousePresent = FALSE;
int PegPosition; /* for keyboard use */
int PegNumber = 0;
int PegAnswer[5];
int PegGuess[5];
int Turn = 1;
int UWin,ULose;
int MissedBox = FALSE;
int MouseValid;
int i;
int NumPegs; /* mode = 0 or 1 -> 6 or 7; mode = 2 -> 8 or 9 */
int BlackNum;
int WhiteNum;
int Loop, CheatToggle, SoundToggle;
int HelpOn = TRUE;
int GraphDriver, GraphMode, ErrorCode;
int PegCoords[4] = {6 , 2 , 10 , 24};
int PutCoords[4] = {20 , 2 , 54 , 24};
int MakeGuessCoords[4] = {61, 1, 78, 1};
int OptionsCoords[4] = {2, 1, 13, 1};
struct text_info initial_info;
void InitScreenSettings(void);
void ShareWareScreen(void);
void PrintDirections(void);
int HiliteMenuEntry(windesc *w, char *entry[], int min_entry,
int max_entry, int spacing);
int HiliteSettingsEntry(windesc *w, int init_entryno);
void InitializeMouse(void);
int NumberOfPegColors(int mode);
void DrawPeg(int mode, int pegnumber, int xloc, int yloc);
void DrawScreen(int mode);
void DrawScreenText(int mode);
void DrawBorder(void);
void DrawPegs(int mode);
void DrawGuessHoles(int turn, int mode);
void RemoveOldIndicator(int mode);
void DrawNewIndicator(int mode);
void Init(void);
void RunMasterMind(void);
void RandomPattern(int mode);
int GetPegType(int mode, char ch);
void SetPegPosition(int mode);
void ClearSetPeg(int mode);
void PutPegType(int mode);
void MakeGuess(int mode);
void GetBlackAndWhiteClues();
void YouWin(void);
void YouLose(void);
void GiveHelpInformation(int msg);
void SettingsMenu(void);
void GiveWinOrLoseStatus(int msg);
void Cheat(int mode);
void Instructions(int select);
void Quit(void);
void BeepHi(void);
void BeepLo(void);
void ShareWareScreen(void)
{
char *share_entries[] = {
"",
" This is a shareware program. You are free to test it, copy",
" it and distribute it. If you expect to play it regularly,",
" please support my programming efforts by registering your",
" copy. To register, send your name, address, and $10 to me",
" at the address listed below. Your comments and suggestions",
" are also welcome.",
"",
" This program requires a color monitor. It has been tested",
" on CGA, EGA and VGA systems. Use of a mouse is optional.",
" A complete set of directions is included.",
"",
" Dan Bartnik",
" 3776 Golden Hill Terrace",
" Eagan, MN 55122",
"",
" < press a key or mouse button to continue >",
};
windesc *share_win;
int i;
if (MousePresent) mouse_off(1);
clrscr();
share_win = draw_win(CTRWIN,CTRWIN,62,18," MASTERMIND - Version 1.0 ",popup,&defcolors);
slct_win(share_win);
for (i=1; i<=16; i++)
prtfstr(1,i,share_entries[i],share_win->wc.text_color,80);
do {} while(!kbhit() && !button_press(LEFT) && !button_press(RIGHT));
if (kbhit()) getch();
if (MousePresent) mouse_on(1);
rmv_win(share_win);
}
void PrintDirections(void)
{
char *menu1_entries[] = {
" Play Mastermind",
" Play Mini Mastermind",
" Play Advanced Mastermind",
" Instructions for Play",
" Return to DOS"
};
windesc *w1;
int i, entryno, x, y;
/* unsigned int key; */
Help = FALSE;
Done = FALSE;
ShowMainMenu = FALSE; /* goes back to TRUE if directions are printed */
NumPegs = 6;
if (MousePresent) mouse_off(1);
textattr(BLUE + (LIGHTGRAY<<4));
clrscr(); /* check for "Play Game" or "Show Help" */
w1 = draw_win(CTRWIN,CTRWIN,28,13," MASTERMIND - Ver 1.0 ",popup,&defcolors);
slct_win(w1);
for (i=1; i<=5; i++) {
prtfstr(1,2*i-1,"",w1->wc.text_color,80);
prtfstr(1,2*i,menu1_entries[i-1],w1->wc.text_color,80);
}
entryno = HiliteMenuEntry(w1,menu1_entries,1,5,2); /* get an entry */
switch(entryno) {
case 1: mode = 0; MaxTurns = 10; break; /* Mastermind */
case 2: mode = 1; MaxTurns = 6; break; /* Mini Mastermind */
case 3: mode = 2; MaxTurns = 12; break; /* Adv Mastermind */
case 4: Help = TRUE; break; /* Print Directions */
case 5: Done = TRUE; break; /* Exit Program */
}
set_mouse_window(1,1,80,25);
rmv_win(w1);
clrscr();
if (Done) Quit();
if (Help)
{ /* PRINT FIRST PART OF DIRECTIONS */
ShowMainMenu = TRUE; /* return to main menu after directions */
textattr(RED + (LIGHTGRAY<<4));
clrscr();
gotoxy(1,1);
cprintf(" INSTRUCTIONS FOR MASTERMIND");
textattr(BLUE + (LIGHTGRAY<<4));
gotoxy(1,2);
cprintf("This game is similar to the board game Mastermind. It is a great exercise in");
gotoxy(1,3);
cprintf("logic. The computer generates a secret code and you must try to determine the");
gotoxy(1,4);
cprintf("code through a series of guesses. After each guess you will be given clues which");
gotoxy(1,5);
cprintf("indicate how close your guess is to the secret code. This game has an advantage");
gotoxy(1,6);
cprintf("over the board game, the computer won't err in presenting clues. The secret");
gotoxy(1,7);
cprintf("code consists of four colored tiles; tiles can take on 6 (or possibly 7) colors.");
gotoxy(1,8);
cprintf("You may receive two types of clues, black and white :");
textattr(BLACK + (LIGHTGRAY<<4)); gotoxy(1,10);
cprintf("██ - one guess is of the correct color and in the correct location.");
textattr(WHITE + (LIGHTGRAY<<4)); gotoxy(1,12);
cprintf("██ - one guess is of the correct color but the location is incorrect.");
textattr(BLUE + (LIGHTGRAY<<4));
gotoxy(1,14);
cprintf("You win if you can determine all four parts of the secret code (this corresponds");
gotoxy(1,15);
cprintf("to getting four BLACK clues) in ten or less turns. When the game begins you will");
gotoxy(1,16);
cprintf("be given the option of having either 6 or 7 types of colored tiles in the secret");
gotoxy(1,17);
cprintf("code; six is for beginners. You may play by using either a mouse, the keyboard,");
gotoxy(1,18);
cprintf("or a combination of the two.");
gotoxy(1,19);
cprintf("MOUSE : Use the left button to play the game. The right button brings a");
gotoxy(1,20);
cprintf(" popup settings menu.");
gotoxy(1,21);
cprintf("KEYBOARD : Use left & right arrow keys to move to a guess location. Enter");
gotoxy(1,22);
cprintf(" the appropriate number to place a tile at that location. The");
gotoxy(1,23);
cprintf(" F1 key brings a popup settings menu.");
textattr(RED + (LIGHTGRAY<<4));
gotoxy(10,25);
cprintf("< press a mouse button or key to see an example game board >");
textattr(WHITE + (LIGHTGRAY<<4));
while ( !mouse_trigger(1) );
/* PRINT SECOND PART OF DIRECTIONS */
clrscr();
DrawScreen(0);
/* PRINT GUESS HOLES */
DrawPeg(0, 0, 20, 24);
DrawPeg(0, 0, 28, 24);
DrawPeg(0, 0, 36, 24);
DrawPeg(0, 0, 44, 24);
textattr(RED + (LIGHTGRAY<<4)); /* draw indicators: guess 1 */
gotoxy(17,24); cprintf("▓");
gotoxy(49,24); cprintf("▓");
Instructions(1); /* print first popup window instructions */
/* PRINT SAMPLE GUESSES */
DrawPeg(0, 4, 20, 24);
DrawPeg(0, 1, 28, 24);
DrawPeg(0, 1, 36, 24);
DrawPeg(0, 3, 44, 24);
/* PRINT SAMPLE ANSWERS */
DrawPeg(0, 3, 20, 3);
DrawPeg(0, 4, 28, 3);
DrawPeg(0, 1, 36, 3);
DrawPeg(0, 4, 44, 3);
/* PRINT SAMPLE CLUES */
textattr(WHITE + (LIGHTGRAY<<4));
textbackground(BLACK);
gotoxy(63,24); cprintf(" ");
textattr(WHITE + (LIGHTGRAY<<4));
gotoxy(66,24); cprintf("██");
gotoxy(69,24); cprintf("██");
textattr(LIGHTGRAY + (LIGHTGRAY<<4)); /* remove indicators: guess 1 */
gotoxy(17,24); cprintf(" ");
gotoxy(49,24); cprintf(" ");
textattr(RED + (LIGHTGRAY<<4)); /* draw indicators: guess 2 */
gotoxy(17,22); cprintf("▓");
gotoxy(49,22); cprintf("▓");
Instructions(2); /* print final popup window instructions */
/* textattr(MAGENTA + (LIGHTGRAY<<4) + BLINK);
gotoxy(18,25); cprintf(" PRESS MOUSE OR KEYBOARD FOR MAIN MENU ");
textattr(WHITE + (LIGHTGRAY<<4));
do {} while(!kbhit() && !button_press(LEFT) && !button_press(RIGHT));
if (kbhit()) getch(); */
clrscr();
}
if (MousePresent) mouse_on(1);
}
int HiliteMenuEntry(windesc *w, char *entry[],int min_entry,int max_entry,int spacing)
/* goes through a menu, hiliting the entries as either the mouse
or the cursor moves. Returns the entry number presently selected
when RTN or MOUSE button is pressed. */
{
int x, y, newx, newy, oldy;
int entryno, Select = FALSE, ChangeMousePos;
unsigned int key;
set_mouse_window(1,1,1,max_entry-min_entry+1);
move_mouse(1,0);
newy = oldy = 1; entryno = min_entry;
do { /* do this until key or mouse event */
/* hilite an entry */
prtfstr(1,entryno*spacing,"%-*.*s",w->wc.hilite_color,80,w->wd-2,w->wd-2,entry[entryno-1]);
do {
ChangeMousePos = FALSE;
if (MousePresent) {
mouse_txt_posn(&newx,&newy);
if (newy != oldy) {
ChangeMousePos = TRUE;
oldy = newy;
}
}
} while ( (!(key = mouse_trigger(1))) && !ChangeMousePos);
/* back to normal */
prtfstr(1,entryno*spacing,"%-*.*s",w->wc.text_color,80,w->wd-2,w->wd-2,entry[entryno-1]);
switch(key) {
case 0x0938: /* 8 or up arrow */
case UPKEY:
entryno = entryno - 1;
if (entryno < min_entry) entryno = max_entry; /* wrap-around */
break;
case 0x0332: /* 2 or down arrow */
case DOWNKEY:
entryno = entryno + 1;
if (entryno > max_entry) entryno = min_entry; /* wrap-around */
break;
case RIGHT_MOUSE_PRESS:
case LEFT_MOUSE_PRESS: /* break and execute appropriate routine */
case CRKEY:
Select = TRUE;
break;
default: /* have hiliting track mouse position */
if (ChangeMousePos) {
mouse_txt_posn(&x,&y);
entryno = y;
break;
}
}
} while (!Select);
return (entryno);
}
int HiliteSettingsEntry(windesc *w, int init_entryno)
/* goes through a menu, hiliting the entries as either the mouse
or the cursor moves. Returns the entry number presently selected
when RTN or MOUSE button is pressed. */
{
int x, y, newx, newy, oldy;
int entryno, Select = FALSE, ChangeMousePos;
unsigned int key;
int min_entry = 1, max_entry = 8;
char msg[30],msg1[30],msg2[30],msg3[30],msg4[30],
msg5[30],msg6[30],msg7[30],msg8[30];
entryno = init_entryno; /* first entry to be hilited */
newy = oldy = 1;
set_mouse_window(1,1,min_entry,max_entry);
move_mouse(1,init_entryno-1);
strcpy(msg1," Restart");
if (SoundToggle) strcpy(msg2," Sound is ON ");
else strcpy(msg2," Sound is OFF");
if (HelpOn) strcpy(msg3," Messages are ON ");
else strcpy(msg3," Messages are OFF");
strcpy(msg4," Cheat");
strcpy(msg5," Instructions");
strcpy(msg6," Return to Main Menu");
strcpy(msg7," Return to DOS");
strcpy(msg8," Exit Menu <ESC>");
do { /* do this until key or mouse event */
switch (entryno) {
case 1: for (i=0;i<30;i++) msg[i] = msg1[i]; break;
case 2: for (i=0;i<30;i++) msg[i] = msg2[i]; break;
case 3: for (i=0;i<30;i++) msg[i] = msg3[i]; break;
case 4: for (i=0;i<30;i++) msg[i] = msg4[i]; break;
case 5: for (i=0;i<30;i++) msg[i] = msg5[i]; break;
case 6: for (i=0;i<30;i++) msg[i] = msg6[i]; break;
case 7: for (i=0;i<30;i++) msg[i] = msg7[i]; break;
case 8: for (i=0;i<30;i++) msg[i] = msg8[i]; break;
}
/* hilite an entry */
prtfstr(1,entryno*2,"%-*.*s",w->wc.hilite_color,80,w->wd-2,w->wd-2,msg);
do {
ChangeMousePos = FALSE;
if (MousePresent) {
mouse_txt_posn(&newx,&newy);
if (newy != oldy) {
ChangeMousePos = TRUE;
oldy = newy;
}
}
} while ( (!(key = mouse_trigger(1))) && !ChangeMousePos);
/* back to normal */
prtfstr(1,entryno*2,"%-*.*s",w->wc.text_color,80,w->wd-2,w->wd-2,msg);
switch(key) {
case 0x0938: /* 8 or up arrow */
case UPKEY:
entryno = entryno - 1;
if (entryno < min_entry) entryno = max_entry; /* wrap-around */
break;
case 0x0332: /* 2 or down arrow */
case DOWNKEY:
entryno = entryno + 1;
if (entryno > max_entry) entryno = min_entry; /* wrap-around */
break;
case 0x011b: /* ESC, remove menu */
Select = TRUE;
entryno = 8;
break;
case RIGHT_MOUSE_PRESS:
case LEFT_MOUSE_PRESS: /* break and execute appropriate routine */
case CRKEY:
Select = TRUE;
move_mouse(1,entryno-1); /* move mouse to entryno position */
break;
default: /* have hiliting track mouse position */
if (ChangeMousePos) {
mouse_txt_posn(&x,&y);
entryno = y;
break;
}
}
} while (!Select);
return (entryno);
}
void InitScreenSettings(void)
{
delay(1); /* call delay, it's not accurate 1st time */
gettextinfo(&initial_info); /* save state of screen */
textmode(3); /* set to color 80 column mode */
TurnCursorOff();
TurnBorderOn(LIGHTGRAY);
textbackground(LIGHTGRAY);
}
void InitializeMouse(void)
{
MousePresent = init_mouse(MOUSE_OPTIONAL, MOUSE_TEXT_MODE, MOUSE_TEXT_MODE);
move_mouse(40,12); /* move mouse to middle of screen */
if (!MousePresent) UseKbd = TRUE;
}
void Init(void)
{
CheatToggle = FALSE;
Turn = 1;
DrawScreen(mode);
if (UseKbd) { PegPosition = 1; SetPegPosition(mode); }
}
int NumberOfPegColors(int mode)
{
char *menu2_entries[] = {
"6 Peg Types",
"7 Peg Types"
};
char *menu3_entries[] = {
"8 Peg Types",
"9 Peg Types",
"10 Peg Types",
"11 Peg Types"
};
windesc *w2;
int i, entryno, x, y, Answer, count;
unsigned int key;
Help = FALSE;
Done = FALSE;
NumPegs = 6;
if (MousePresent) mouse_off(1);
switch (mode) {
case 0:
w2 = draw_win(CTRWIN,CTRWIN,28,7," MASTERMIND ",popup,&defcolors);
break;
case 1:
w2 = draw_win(CTRWIN,CTRWIN,28,7," MINI MASTERMIND ",popup,&defcolors);
break;
case 2:
w2 = draw_win(CTRWIN,CTRWIN,28,11," ADVANCED MASTERMIND ",popup,&defcolors);
break;
}
slct_win(w2);
if (mode == 2) count = 4; else count = 2;
for (i=1; i<=count; i++) {
prtfstr(1,2*i-1,"",w2->wc.text_color,80);
if ((mode == 0) || (mode == 1))
prtfstr(1,2*i,menu2_entries[i-1],w2->wc.text_color,80);
else if (mode == 2)
prtfstr(1,2*i,menu3_entries[i-1],w2->wc.text_color,80);
}
if ((mode == 0) || (mode == 1))
entryno = HiliteMenuEntry(w2,menu2_entries,1,2,2); /* mode 0 or 1 */
else if (mode == 2)
entryno = HiliteMenuEntry(w2,menu3_entries,1,4,2); /* mode 2 */
if ((mode == 0) || (mode == 1)) {
if (entryno == 1) Answer = 6;
else if (entryno == 2) Answer = 7;
}
else if (mode == 2) {
if (entryno == 1) Answer = 8;
else if (entryno == 2) Answer = 9;
else if (entryno == 3) Answer = 10;
else if (entryno == 4) Answer = 11;
}
if (MousePresent) mouse_on(1);
set_mouse_window(1,1,80,25);
rmv_win(w2);
move_mouse(40,12); /* move mouse to middle of screen */
return(Answer);
}
void DrawPeg(int mode, int pegnumber, int xloc, int yloc)
{
int color[] = {0,BLACK,BLUE,GREEN,CYAN,RED,MAGENTA,BROWN,BROWN,BROWN};
mouse_off(1);
gotoxy(xloc,yloc);
if (pegnumber <=7) textattr(WHITE + (color[pegnumber]<<4));
if ((mode == 1) && (pegnumber >= 1)) { /* Mini Mastermind */
cprintf(" %1d ",pegnumber);
gotoxy(xloc,yloc-1);
cprintf(" ");
}
else if (((mode==0)||(mode==2)) && ((pegnumber>=1)&&(pegnumber<=7))) {
cprintf(" %1d ",pegnumber); /* Mastermind or Adv Mastermind */
}
else if ((mode == 2) && (pegnumber == 8)) { /* Adv Mastermind only */
textattr(RED + (LIGHTGRAY<<4));
cprintf("▒▒▒");
gotoxy(xloc+1,yloc);
textattr(WHITE + (LIGHTGRAY<<4));
cprintf("8");
}
else if ((mode == 2) && (pegnumber == 9)) { /* Adv Mastermind only */
textattr(BLACK + (LIGHTGRAY<<4));
cprintf("▒▒▒");
gotoxy(xloc+1,yloc);
textattr(WHITE + (LIGHTGRAY<<4));
cprintf("9");
}
else if ((mode == 2) && (pegnumber == 10)) { /* Adv Mastermind only */
textattr(BLUE + (LIGHTGRAY<<4));
cprintf("▒▒▒");
gotoxy(xloc+1,yloc);
textattr(WHITE + (LIGHTGRAY<<4));
cprintf("A");
}
else if ((mode == 2) && (pegnumber == 11)) { /* Adv Mastermind only */
textattr(GREEN + (LIGHTGRAY<<4));
cprintf("▒▒▒");
gotoxy(xloc+1,yloc);
textattr(WHITE + (LIGHTGRAY<<4));
cprintf("B");
}
else if (pegnumber == 0) { /* clear out a peg */
textattr(LIGHTCYAN + (LIGHTGRAY<<4));
if (mode == 1) {
cprintf("▒▒▒▒▒");
gotoxy(xloc,yloc-1);
cprintf("▒▒▒▒▒");
}
else if ((mode == 0) || (mode == 2)) cprintf("▒▒▒");
}
else if (pegnumber == -1) { /* draw semi clear pegs */
textattr(LIGHTCYAN + (LIGHTGRAY<<4));
if (mode == 1) {
cprintf("░░░░░");
gotoxy(xloc,yloc-1);
cprintf("░░░░░");
}
else if ((mode == 0) || (mode == 2)) cprintf("░░░");
}
mouse_on(1);
}
void DrawScreen(int mode)
{
if (MousePresent) mouse_off(1);
textattr(RED + (LIGHTGRAY<<4));
clrscr();
DrawBorder();
DrawScreenText(mode);
DrawPegs(mode);
DrawGuessHoles(mode,Turn);
if (MousePresent) mouse_on(1);
}
void DrawScreenText(int mode)
{
textattr(RED + (LIGHTGRAY<<4));
switch (mode) {
case 0:
gotoxy(31,1);
cprintf(" MASTERMIND ");
break;
case 1:
gotoxy(29,1);
cprintf(" MINI MASTERMIND ");
break;
case 2:
gotoxy(27,1);
cprintf(" ADVANCED MASTERMIND ");
break;
}
textattr(WHITE + (BLUE<<4));
gotoxy(2,1); cprintf("OPTIONS <F1>");
gotoxy(61,1); cprintf(" MAKE GUESS <RTN> ");
textattr(WHITE + (BLUE<<4)); textbackground(LIGHTGRAY);
}
void DrawBorder(void)
{
int x,y;
textattr(WHITE + (LIGHTGRAY<<4));
x = 1;
for (y = 2; y <= 24; y++) { gotoxy(x,y); cprintf("║"); };
x = 15;
for (y = 2; y <= 24 ; y++) { gotoxy(x,y); cprintf("║"); };
x = 59;
for (y = 2; y <= 24 ; y++) { gotoxy(x,y); cprintf("║"); };
x = 79;
for (y = 2; y <= 24 ; y++) { gotoxy(x,y); cprintf("║"); };
y = 1;
for (x = 2; x <= 78 ; x++) { gotoxy(x,y); cprintf("═"); };
y = 25;
for (x = 2; x <= 78 ; x++) { gotoxy(x,y); cprintf("═"); };
gotoxy( 1, 1); cprintf("╔");
gotoxy(15, 1); cprintf("╦");
gotoxy(59, 1); cprintf("╦");
gotoxy(79, 1); cprintf("╗");
gotoxy( 1,25); cprintf("╚");
gotoxy(15,25); cprintf("╩");
gotoxy(59,25); cprintf("╩");
gotoxy(79,25); cprintf("╝");
}
void DrawPegs(int mode)
{
int i;
if (mode==1) {
for (i=1; i <= NumPegs; i++) DrawPeg(mode, i, 6, 27 - 3*i);
DrawPeg(mode, 0, 6, 27 - 3*(NumPegs+1)); /* draw a blank peg here */
}
else if ((mode == 0) || (mode == 2)) {
for (i=1; i <= NumPegs; i++) DrawPeg(mode, i, 6, 26 - 2*i);
DrawPeg(mode, 0, 6, 26 - 2*(NumPegs+1)); /* draw a blank peg here */
}
}
void DrawGuessHoles(int mode,int turn) /* draw guess holes for present turn */
{
int x, y, t;
if (mode == 1) { /* Mini Mastermind */
if (turn <= MaxTurns)
for (x = 0 ; x <= 3 ; x++) DrawPeg(mode, 0, 20+10*x, 27-3*turn);
if (turn==1) for (x = 0 ; x <= 3 ; x++)
for (t = 2 ; t <= 6 ; t++) DrawPeg(mode, -1, 20+10*x, 27-3*t);
}
else if (mode == 0) { /* Mastermind */
if (turn <= MaxTurns)
for (x = 0 ; x <= 3 ; x++) DrawPeg(mode, 0, 20+8*x, 26-2*turn);
if (turn==1) for (x = 0 ; x <= 3 ; x++)
for (t = 2 ; t <= 10 ; t++) DrawPeg(mode, -1, 20+8*x, 26-2*t);
}
else if (mode == 2) { /* Adv Mastermind */
if (turn <= MaxTurns)
for (x = 0 ; x <= 4 ; x++) DrawPeg(mode, 0, 20+8*x, 26-2*turn);
if (turn==1) for (x = 0 ; x <= 4 ; x++)
for (t = 2 ; t <= 12 ; t++) DrawPeg(mode, -1, 20+8*x, 26-2*t);
}
}
void RemoveOldIndicator(int mode)
{
if (mode == 1) { /* Mini Mastermind */
textattr(LIGHTGRAY + (LIGHTGRAY<<4)); /* clear old indicator */
gotoxy(17 , (26 - 3*Turn) ); cprintf(" ");
gotoxy(17 , (27 - 3*Turn) ); cprintf(" ");
gotoxy(57 , (26 - 3*Turn) ); cprintf(" ");
gotoxy(57 , (27 - 3*Turn) ); cprintf(" ");
}
else if (mode == 0) { /* Mastermind */
textattr(LIGHTGRAY + (LIGHTGRAY<<4)); /* clear old indicator */
gotoxy(17 , (26 - 2*Turn) ); cprintf(" ");
gotoxy(49 , (26 - 2*Turn) ); cprintf(" ");
}
if (mode == 2) { /* Adv Mastermind */
textattr(LIGHTGRAY + (LIGHTGRAY<<4)); /* clear old indicator */
gotoxy(17 , (26 - 2*Turn) ); cprintf(" ");
gotoxy(57 , (26 - 2*Turn) ); cprintf(" ");
}
}
void DrawNewIndicator(int mode)
{
if (Turn <= MaxTurns) {
if (mode == 1) { /* Mini Mastermind */
textattr(RED + (LIGHTGRAY<<4)); /* draw new indicator */
gotoxy(17 , (26 - 3*Turn) ); cprintf("▓");
gotoxy(17 , (27 - 3*Turn) ); cprintf("▓");
gotoxy(57 , (26 - 3*Turn) ); cprintf("▓");
gotoxy(57 , (27 - 3*Turn) ); cprintf("▓");
}
else if (mode == 0) { /* Mastermind */
textattr(RED + (LIGHTGRAY<<4)); /* draw new indicator */
gotoxy(17 , (26 - 2*Turn) ); cprintf("▓");
gotoxy(49 , (26 - 2*Turn) ); cprintf("▓");
}
else if (mode == 2) { /* Adv Mastermind */
textattr(RED + (LIGHTGRAY<<4)); /* draw new indicator */
gotoxy(17 , (26 - 2*Turn) ); cprintf("▓");
gotoxy(57 , (26 - 2*Turn) ); cprintf("▓");
}
}
}
void RandomPattern(int mode)
{
int i;
randomize();
if (mode == 0 || mode == 1) { /* Mastermind & Mini Mastermind */
PegAnswer[4] = 0;
for (i = 0 ; i <= 3 ; i++) PegAnswer[i] = 1 + random(NumPegs);
}
else if (mode == 2) /* Adv Mastermind */
for (i = 0 ; i <= 4 ; i++) PegAnswer[i] = 1 + random(NumPegs);
}
int GetPegType(int mode, char ch)
/* make selected peg blink; return selected peg */
{
if (mode == 1) { /* Mini Mastermind */
if ( ((ch >= 48) && (ch <= NumPegs + 48)) || MouseValid && (
mouse_in_box(0, 6, 23-3*NumPegs, 10, 24-3*NumPegs) ||
mouse_in_box(0, 6 , 23 , 10 , 24) ||
mouse_in_box(0, 6 , 20 , 10 , 21) ||
mouse_in_box(0, 6 , 17 , 10 , 18) ||
mouse_in_box(0, 6 , 14 , 10 , 15) ||
mouse_in_box(0, 6 , 11 , 10 , 12) ||
mouse_in_box(0, 6 , 8 , 10 , 9 ) ||
((NumPegs == 7) && (mouse_in_box(0, 6 , 5 , 10 , 6 )))) ) {
if (MousePresent) mouse_off(1);
DrawPegs(mode); /* stops pegs which were blinking from doing so */
if (MousePresent) mouse_on(1);
}
if (ch==48||(MouseValid && mouse_in_box(0,6,23-3*NumPegs,10,24-3*NumPegs))) {
if (MousePresent) mouse_off(1);
textattr(LIGHTCYAN + (LIGHTGRAY<<4) + BLINK);
gotoxy(6, 23-3*NumPegs); cprintf("▒▒▒▒▒");
gotoxy(6, 24-3*NumPegs); cprintf("▒▒▒▒▒");
if (MousePresent) mouse_on(1);
return(99);
}
else if (ch == 49 || (MouseValid && mouse_in_box(0, 6 , 23 , 10 , 24))) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (BLACK<<4) + BLINK);
gotoxy(6,23); cprintf(" ");
gotoxy(6,24); cprintf(" 1 ");
if (MousePresent) mouse_on(1);
return(1);
}
else if (ch == 50 || (MouseValid && mouse_in_box(0, 6 , 20 , 10 , 21))) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (BLUE<<4) + BLINK);
gotoxy(6,20); cprintf(" ");
gotoxy(6,21); cprintf(" 2 ");
if (MousePresent) mouse_on(1);
return(2);
}
else if (ch == 51 || (MouseValid && mouse_in_box(0, 6 , 17 , 10 , 18))) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (GREEN<<4) + BLINK);
gotoxy(6,17); cprintf(" ");
gotoxy(6,18); cprintf(" 3 ");
if (MousePresent) mouse_on(1);
return(3);
}
else if (ch == 52 || (MouseValid && mouse_in_box(0, 6 , 14 , 10 , 15))) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (CYAN<<4) + BLINK);
gotoxy(6,14); cprintf(" ");
gotoxy(6,15); cprintf(" 4 ");
if (MousePresent) mouse_on(1);
return(4);
}
else if (ch == 53 || (MouseValid && mouse_in_box(0, 6 , 11 , 10 , 12))) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (RED<<4) + BLINK);
gotoxy(6,11); cprintf(" ");
gotoxy(6,12); cprintf(" 5 ");
if (MousePresent) mouse_on(1);
return(5);
}
else if (ch == 54 || (MouseValid && mouse_in_box(0, 6 , 8 , 10 , 9 ))) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (MAGENTA<<4) + BLINK);
gotoxy(6,8); cprintf(" ");
gotoxy(6,9); cprintf(" 6 ");
if (MousePresent) mouse_on(1);
return(6);
}
else if ( (NumPegs == 7) &&
(ch == 55 || (MouseValid && mouse_in_box(0, 6 , 5 , 10 , 6 ))) ) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (BROWN<<4) + BLINK);
gotoxy(6,5); cprintf(" ");
gotoxy(6,6); cprintf(" 7 ");
if (MousePresent) mouse_on(1);
return(7);
}
else return(0); /* the selection missed all the peg boxes */
}
else if ((mode == 0) || (mode == 2)) { /* Mastermind or Adv Mastermind */
if ( ((ch >= 48) && (ch <= 98)) || MouseValid && (
mouse_in_box(0, 6, 24-2*NumPegs, 8, 24-2*NumPegs) || /* blank peg */
mouse_in_box(0, 6 , 24 , 8 , 24) ||
mouse_in_box(0, 6 , 22 , 8 , 22) ||
mouse_in_box(0, 6 , 20 , 8 , 20) ||
mouse_in_box(0, 6 , 18 , 8 , 18) ||
mouse_in_box(0, 6 , 16 , 8 , 16) ||
mouse_in_box(0, 6 , 14 , 8 , 14 ) ||
((NumPegs >= 7) && (mouse_in_box(0, 6 , 12 , 8 , 12 ))) ||
((NumPegs >= 8) && (mouse_in_box(0, 6 , 10 , 8 , 10 ))) ||
((NumPegs >= 9) && (mouse_in_box(0, 6 , 8 , 8 , 8 ))) ||
((NumPegs >=10) && (mouse_in_box(0, 6 , 6 , 8 , 6 ))) ||
((NumPegs ==11) && (mouse_in_box(0, 6 , 4 , 8 , 4 )))) ) {
if (MousePresent) mouse_off(1);
DrawPegs(mode); /* stops pegs which were blinking from doing so */
if (MousePresent) mouse_on(1);
}
if (ch==48||(MouseValid && mouse_in_box(0,6,24-2*NumPegs,8,24-2*NumPegs))) {
if (MousePresent) mouse_off(1);
textattr(LIGHTCYAN + (LIGHTGRAY<<4) + BLINK);
gotoxy(6,24-2*NumPegs); cprintf("▒▒▒");
if (MousePresent) mouse_on(1);
return(99);
}
else if (ch == 49 || (MouseValid && mouse_in_box(0, 6 , 24 , 8 , 24))) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (BLACK<<4) + BLINK);
gotoxy(6,24); cprintf(" 1 ");
if (MousePresent) mouse_on(1);
return(1);
}
else if (ch == 50 || (MouseValid && mouse_in_box(0, 6 , 22 , 8 , 22))) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (BLUE<<4) + BLINK);
gotoxy(6,22); cprintf(" 2 ");
if (MousePresent) mouse_on(1);
return(2);
}
else if (ch == 51 || (MouseValid && mouse_in_box(0, 6 , 20 , 8 , 20))) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (GREEN<<4) + BLINK);
gotoxy(6,20); cprintf(" 3 ");
if (MousePresent) mouse_on(1);
return(3);
}
else if (ch == 52 || (MouseValid && mouse_in_box(0, 6 , 18 , 8 , 18))) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (CYAN<<4) + BLINK);
gotoxy(6,18); cprintf(" 4 ");
if (MousePresent) mouse_on(1);
return(4);
}
else if (ch == 53 || (MouseValid && mouse_in_box(0, 6 , 16 , 8 , 16))) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (RED<<4) + BLINK);
gotoxy(6,16); cprintf(" 5 ");
if (MousePresent) mouse_on(1);
return(5);
}
else if (ch == 54 || (MouseValid && mouse_in_box(0, 6 , 14 , 8 , 14))) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (MAGENTA<<4) + BLINK);
gotoxy(6,14); cprintf(" 6 ");
if (MousePresent) mouse_on(1);
return(6);
}
else if ( (NumPegs >= 7) &&
(ch == 55 || (MouseValid && mouse_in_box(0, 6 , 12 , 8 , 12))) ) {
if (MousePresent) mouse_off(1);
textattr(WHITE + (BROWN<<4) + BLINK);
gotoxy(6,12); cprintf(" 7 ");
if (MousePresent) mouse_on(1);
return(7);
}
else if ( (NumPegs >= 8) &&
(ch == 56 || (MouseValid && mouse_in_box(0, 6 , 10 , 8 , 10))) ) {
if (MousePresent) mouse_off(1);
textattr(RED + (LIGHTGRAY<<4));
gotoxy(6,10); cprintf("▒▒▒");
textattr(WHITE + (LIGHTGRAY<<4) + BLINK);
gotoxy(7,10); cprintf("8");
if (MousePresent) mouse_on(1);
return(8);
}
else if ( (NumPegs >= 9) &&
(ch == 57 || (MouseValid && mouse_in_box(0, 6 , 8 , 8 , 8))) ) {
if (MousePresent) mouse_off(1);
textattr(BLACK + (LIGHTGRAY<<4));
gotoxy(6,8); cprintf("▒▒▒");
textattr(WHITE + (LIGHTGRAY<<4) + BLINK);
gotoxy(7,8); cprintf("9");
if (MousePresent) mouse_on(1);
return(9);
}
else if ( (NumPegs >= 10) &&
((ch==65) || (ch==97) || (MouseValid && mouse_in_box(0, 6 , 6 , 8 , 6))) ) {
if (MousePresent) mouse_off(1);
textattr(BLUE + (LIGHTGRAY<<4));
gotoxy(6,6); cprintf("▒▒▒");
textattr(WHITE + (LIGHTGRAY<<4) + BLINK);
gotoxy(7,6); cprintf("A");
if (MousePresent) mouse_on(1);
return(10);
}
else if ( (NumPegs >= 11) &&
((ch==66) || (ch==98) || (MouseValid && mouse_in_box(0, 6 , 4 , 8 , 4))) ) {
if (MousePresent) mouse_off(1);
textattr(GREEN + (LIGHTGRAY<<4));
gotoxy(6,4); cprintf("▒▒▒");
textattr(WHITE + (LIGHTGRAY<<4) + BLINK);
gotoxy(7,4); cprintf("B");
if (MousePresent) mouse_on(1);
return(11);
}
else return(0); /* the selection missed all the peg boxes */
}
}
void SetPegPosition(int mode)
/* This function positions the arrow indicators > & < when keyboard
entry is used. */
{
if (MousePresent) mouse_off(1);
if (mode == 1) { /* Mini Mastermind */
if (FunctionKey && ch == 75) { /* left arrow */
PegPosition--;
if (PegPosition == 0) PegPosition = 4;
}
else if (FunctionKey && ch == 77) { /* right arrow */
PegPosition++;
if (PegPosition == 5) PegPosition = 1;
}
textattr(RED + (LIGHTGRAY<<4));
ClearSetPeg(mode);
if (Turn <= 6) {
gotoxy(9 + 10*PegPosition , (27 - 3*Turn) ); cprintf(">");
gotoxy(15 + 10*PegPosition , (27 - 3*Turn) ); cprintf("<");
}
}
else if (mode == 0) { /* Mastermind */
if (FunctionKey && ch == 75) { /* left arrow */
PegPosition--;
if (PegPosition == 0) PegPosition = 4;
}
else if (FunctionKey && ch == 77) { /* right arrow */
PegPosition++;
if (PegPosition == 5) PegPosition = 1;
}
textattr(RED + (LIGHTGRAY<<4));
ClearSetPeg(mode);
if (Turn <= 10) {
gotoxy(11 + 8*PegPosition , (26 - 2*Turn) ); cprintf(">");
gotoxy(15 + 8*PegPosition , (26 - 2*Turn) ); cprintf("<");
}
}
else if (mode == 2) { /* Adv Mastermind */
if (FunctionKey && ch == 75) { /* left arrow */
PegPosition--;
if (PegPosition == 0) PegPosition = 5;
}
else if (FunctionKey && ch == 77) { /* right arrow */
PegPosition++;
if (PegPosition == 6) PegPosition = 1;
}
textattr(RED + (LIGHTGRAY<<4));
ClearSetPeg(mode);
if (Turn <= 12) {
gotoxy(11 + 8*PegPosition , (26 - 2*Turn) ); cprintf(">");
gotoxy(15 + 8*PegPosition , (26 - 2*Turn) ); cprintf("<");
}
}
if (MousePresent) mouse_on(1);
}
void ClearSetPeg(int mode)
{
if (mode == 1) { /* Mini Mastermind */
if (Turn > 1) { /* clear markers from previous guess */
for (i=19; i<=49; i=i+10) {gotoxy(i,(27-3*(Turn-1)) ); cprintf(" "); }
for (i=25; i<=55; i=i+10) {gotoxy(i,(27-3*(Turn-1)) ); cprintf(" "); }
}
for (i = 19; i <= 49; i=i+10) { gotoxy(i, (27 - 3*Turn) ); cprintf(" ");}
for (i = 25; i <= 55; i=i+10) { gotoxy(i, (27 - 3*Turn) ); cprintf(" ");}
}
else if (mode == 0) { /* Mastermind */
if (Turn > 1) { /* clear markers from previous guess */
for (i=19; i<=43; i=i+8) {gotoxy(i,(26-2*(Turn-1)) ); cprintf(" "); }
for (i=23; i<=47; i=i+8) {gotoxy(i,(26-2*(Turn-1)) ); cprintf(" "); }
}
for (i = 19; i <= 43; i=i+8) { gotoxy(i, (26 - 2*Turn) ); cprintf(" ");}
for (i = 23; i <= 47; i=i+8) { gotoxy(i, (26 - 2*Turn) ); cprintf(" ");}
}
else if (mode == 2) { /* Adv Mastermind */
if (Turn > 1) { /* clear markers from previous guess */
for (i=19; i<=51; i=i+8) {gotoxy(i,(26-2*(Turn-1)) ); cprintf(" "); }
for (i=23; i<=55; i=i+8) {gotoxy(i,(26-2*(Turn-1)) ); cprintf(" "); }
}
for (i = 19; i <= 51; i=i+8) { gotoxy(i, (26 - 2*Turn) ); cprintf(" ");}
for (i = 23; i <= 55; i=i+8) { gotoxy(i, (26 - 2*Turn) ); cprintf(" ");}
}
}
void PutPegType(int mode) /* place selected peg in box; undo blinking peg */
{
MissedBox = FALSE;
if (mode == 1) { /* Mini Mastermind */
if (((ch >= 48) && (ch <= NumPegs+48) && PegPosition == 1)
|| MouseValid && mouse_in_box(0,20,(26 - 3*Turn),24,(27 - 3*Turn) ))
{
if (MousePresent) mouse_off(1);
if (PegNumber == 99) {
DrawPeg(mode, 0, 20, 27-3*Turn);
PegGuess[0] = 0;
}
else {
PegGuess[0] = PegNumber;
DrawPeg(mode, PegNumber, 20, 27-3*Turn);
}
}
else if (((ch >= 48) && (ch <= NumPegs+48) && PegPosition == 2)
|| MouseValid && mouse_in_box(0,30,(26 - 3*Turn),34,(27 - 3*Turn) ))
{
if (MousePresent) mouse_off(1);
if (PegNumber == 99) {
DrawPeg(mode, 0, 30, 27-3*Turn);
PegGuess[1]= 0;
}
else {
PegGuess[1] = PegNumber;
DrawPeg(mode, PegNumber, 30, 27-3*Turn);
}
}
else if (((ch >= 48) && (ch <= NumPegs+48) && PegPosition == 3)
|| MouseValid && mouse_in_box(0,40,(26 - 3*Turn),44,(27 - 3*Turn) ))
{
if (MousePresent) mouse_off(1);
if (PegNumber == 99) {
DrawPeg(mode, 0, 40, 27-3*Turn);
PegGuess[2]= 0;
}
else {
PegGuess[2] = PegNumber;
DrawPeg(mode, PegNumber, 40, 27-3*Turn);
}
}
else if (((ch >= 48) && (ch <= NumPegs+48) && PegPosition == 4)
|| MouseValid && mouse_in_box(0,50,(26 - 3*Turn),54,(27 - 3*Turn) ))
{
if (MousePresent) mouse_off(1);
if (PegNumber == 99) {
DrawPeg(mode, 0, 50, 27-3*Turn);
PegGuess[3]= 0;
}
else {
PegGuess[3] = PegNumber;
DrawPeg(mode, PegNumber, 50, 27-3*Turn);
}
}
}
else if ((mode == 0)||(mode == 2)) { /* Mastermind or Adv Mastermind */
if (((ch >= 48) && (ch <= 98) && PegPosition == 1)
|| MouseValid && mouse_in_box(0,20,(26 - 2*Turn),22,(26 - 2*Turn) ))
{
if (MousePresent) mouse_off(1);
if (PegNumber == 99) {
DrawPeg(mode, 0, 20, 26-2*Turn);
PegGuess[0]= 0;
}
else {
PegGuess[0] = PegNumber;
DrawPeg(mode, PegNumber, 20, 26-2*Turn);
}
}
else if (((ch >= 48) && (ch <= 98) && PegPosition == 2)
|| MouseValid && mouse_in_box(0,28,(26 - 2*Turn),30,(26 - 2*Turn) ))
{
if (MousePresent) mouse_off(1);
if (PegNumber == 99) {
DrawPeg(mode, 0, 28, 26-2*Turn);
PegGuess[1]= 0;
}
else {
PegGuess[1] = PegNumber;
DrawPeg(mode, PegNumber, 28, 26-2*Turn);
}
}
else if (((ch >= 48) && (ch <= 98) && PegPosition == 3)
|| MouseValid && mouse_in_box(0,36,(26 - 2*Turn),38,(26 - 2*Turn) ))
{
if (MousePresent) mouse_off(1);
if (PegNumber == 99) {
DrawPeg(mode, 0, 36, 26-2*Turn);
PegGuess[2]= 0;
}
else {
PegGuess[2] = PegNumber;
DrawPeg(mode, PegNumber, 36, 26-2*Turn);
}
}
else if (((ch >= 48) && (ch <= 98) && PegPosition == 4)
|| MouseValid && mouse_in_box(0,44,(26 - 2*Turn),46,(26 - 2*Turn) ))
{
if (MousePresent) mouse_off(1);
if (PegNumber == 99) {
DrawPeg(mode, 0, 44, 26-2*Turn);
PegGuess[3]= 0;
}
else {
PegGuess[3] = PegNumber;
DrawPeg(mode, PegNumber, 44, 26-2*Turn);
}
}
else if ((mode == 2) && (((ch >= 48) && (ch <= 98) && PegPosition == 5)
|| MouseValid && mouse_in_box(0,52,(26 - 2*Turn),54,(26 - 2*Turn) )))
{
if (MousePresent) mouse_off(1);
if (PegNumber == 99) {
DrawPeg(mode, 0, 52, 26-2*Turn);
PegGuess[4]= 0;
}
else {
PegGuess[4] = PegNumber;
DrawPeg(mode, PegNumber, 52, 26-2*Turn);
}
}
}
else MissedBox = TRUE;
if (MissedBox && HelpOn) {
if (PegNumber == 0) GiveHelpInformation(1);
else if (PegGuess[0] != 0 && PegGuess[1] != 0
&& PegGuess[2] != 0 && PegGuess[3] != 0) GiveHelpInformation(2);
else GiveHelpInformation(3); }
if (MousePresent) mouse_on(1);
}
void MakeGuess(int mode)
{
int x;
if (MousePresent) mouse_off(1);
/* Mini Mastermind */
if ((mode == 1) && (PegGuess[0] != 0 && PegGuess[1] != 0
&& PegGuess[2] != 0 && PegGuess[3] != 0)) {
GetBlackAndWhiteClues(); /* sets BlackNum & WhiteNum */
textbackground(BLACK); /* place black clues */
for (x = 0 ; x <= (BlackNum - 1) ; x++) {
gotoxy(63 + 3*x , 27 - (3*Turn));
cprintf(" ");
}
textattr(WHITE + (LIGHTGRAY<<4)); /* place white clues */
for (x = BlackNum + 1 ; x <= (BlackNum + WhiteNum) ; x++) {
gotoxy(60 + 3*x , 27 - (3*Turn));
cprintf("██");
}
RemoveOldIndicator(mode);
ClearSetPeg(mode);
Turn++;
if (UseKbd) SetPegPosition(mode);
DrawNewIndicator(mode);
DrawGuessHoles(mode,Turn);
if ((Turn == 7) && (BlackNum != 4)) YouLose();
if ((BlackNum == 4)) YouWin();
for (i = 0 ; i <= 3 ; i++) PegGuess[i] = 0;
if ( !ULose && !UWin ) BeepHi();
}
/* Mastermind */
else if ((mode == 0) && (PegGuess[0] != 0 && PegGuess[1] != 0
&& PegGuess[2] != 0 && PegGuess[3] != 0)) {
GetBlackAndWhiteClues(); /* sets BlackNum & WhiteNum */
textbackground(BLACK); /* place black clues */
for (x = 0 ; x <= (BlackNum - 1) ; x++) {
gotoxy(63 + 3*x , 26 - (2*Turn));
cprintf(" ");
}
textattr(WHITE + (LIGHTGRAY<<4)); /* place white clues */
for (x = BlackNum + 1 ; x <= (BlackNum + WhiteNum) ; x++) {
gotoxy(60 + 3*x , 26 - (2*Turn));
cprintf("██");
}
RemoveOldIndicator(mode);
ClearSetPeg(mode);
Turn++;
if (UseKbd) SetPegPosition(mode);
DrawNewIndicator(mode);
DrawGuessHoles(mode,Turn);
if ((Turn == 11) && (BlackNum != 4)) YouLose();
if ((BlackNum == 4)) YouWin();
for (i = 0 ; i <= 3 ; i++) PegGuess[i] = 0;
if ( !ULose && !UWin ) BeepHi();
}
/* Adv Mastermind */
else if ((mode == 2) && (PegGuess[0] != 0 && PegGuess[1] != 0
&& PegGuess[2] != 0 && PegGuess[3] != 0 && PegGuess[4] != 0)) {
GetBlackAndWhiteClues(); /* sets BlackNum & WhiteNum */
textbackground(BLACK); /* place black clues */
for (x = 0 ; x <= (BlackNum - 1) ; x++) {
gotoxy(63 + 3*x , 26 - (2*Turn));
cprintf(" ");
}
textattr(WHITE + (LIGHTGRAY<<4)); /* place white clues */
for (x = BlackNum + 1 ; x <= (BlackNum + WhiteNum) ; x++) {
gotoxy(60 + 3*x , 26 - (2*Turn));
cprintf("██");
}
RemoveOldIndicator(mode);
ClearSetPeg(mode);
Turn++;
if (UseKbd) SetPegPosition(mode);
DrawNewIndicator(mode);
DrawGuessHoles(mode,Turn);
if ((Turn == 13) && (BlackNum != 5)) YouLose();
if ((BlackNum == 5)) YouWin();
for (i = 0 ; i <= 4 ; i++) PegGuess[i] = 0;
if ( !ULose && !UWin ) BeepHi();
}
else /* not time to MAKE GUESS */
{
if (HelpOn) GiveHelpInformation(4);
}
if (MousePresent) mouse_on(1);
}
void GetBlackAndWhiteClues() /* sets BlackNum & WhiteNum */
{
int i, j, k; /* i & j for whitenum, k for blacknum */
int UsedWhiteAnswer[5], UsedWhiteGuess[5], Pegs;
if ((mode == 0) || (mode == 1)) Pegs = 4; else Pegs = 5;
BlackNum = 0; WhiteNum = 0;
for (i = 0 ; i <= Pegs-1 ; i++) {
UsedWhiteAnswer[i] = FALSE;
UsedWhiteGuess[i] = FALSE;
}
for (k=0; k <= Pegs-1; k++) if (PegGuess[k] == PegAnswer[k]) BlackNum++;
for (i = 0 ; i <= Pegs-1 ; i++) {
for (j = 0 ; j <= Pegs-1 ; j++) {
if ((PegGuess[i] != PegAnswer[i]) && (PegGuess[j] != PegAnswer[j])) {
if ((!UsedWhiteGuess[j]) && (!UsedWhiteAnswer[i])) {
if ( PegGuess[j] == PegAnswer[i] ) {
WhiteNum++;
UsedWhiteAnswer[i] = TRUE;
UsedWhiteGuess[j] = TRUE;
}
}
}
}
}
}
void YouLose(void)
{
if (MousePresent) mouse_off(1);
ULose = TRUE;
BeepLo();
GiveWinOrLoseStatus(0); /* YOU LOST message */
if (MousePresent) mouse_on(1);
}
void YouWin(void)
{
int i;
if (MousePresent) mouse_off(1);
UWin = TRUE;
if (HasCheated)
{
BeepLo();
GiveWinOrLoseStatus(2); /* NEXT TIME ... DON'T CHEAT message */
}
else
{
for (i = 1 ; i <= 3 ; i++) BeepHi();
GiveWinOrLoseStatus(1); /* YOU WON message */
}
if (MousePresent) mouse_on(1);
}
void GiveHelpInformation(int msg)
{
char *help_entries[] = { /* each message is 3 lines long */
"",
"MOUSE: Select peg (on left), then place it", /* msg = 1 */
"on board (below). KEYBOARD: Use left &",
"right arrows & number keys.",
"If you are ready to make a guess select", /* msg = 2 */
"MAKE GUESS. If not, you may still change",
"any of your guesses.",
"EITHER place the selected peg on the board",/* msg = 3 */
"OR select a different peg, and then place",
"it on the board.",
"You are not allowed to make a guess", /* msg = 4 */
"until you have placed a guess in each",
"of the guess locations.",
};
windesc *help_win;
int i,j;
if (MousePresent) mouse_off(1);
help_win = draw_win(CTRWIN,CTRWIN,44,5," HELP ",popup,&msgcolors);
slct_win(help_win);
for (i=3*msg-2,j=1; i<=3*msg; i++,j++) prtfstr(1,j,help_entries[i],help_win->wc.text_color,80);
BeepHi();
if (MousePresent) mouse_on(1);
while(!kbhit() && !button_press(LEFT) && !button_press(RIGHT));
if (kbhit()) getch();
rmv_win(help_win);
}
void SettingsMenu(void)
{
windesc *w1;
int i, entryno = 1, init_entryno, x, y, Done = FALSE, mousex, mousey;
unsigned int key;
char msg[30],msg1[30],msg2[30],msg3[30],msg4[30],
msg5[30],msg6[30],msg7[30],msg8[30];
mouse_txt_posn(&mousex,&mousey); /* save mouse's original position */
if (MousePresent) mouse_off(1);
w1 = draw_win(26,CTRWIN,23,19," SETTINGS ",popup,&defcolors);
slct_win(w1);
strcpy(msg1," Restart");
if (SoundToggle) strcpy(msg2," Sound is ON ");
else strcpy(msg2," Sound is OFF");
if (HelpOn) strcpy(msg3," Messages are ON ");
else strcpy(msg3," Messages are OFF");
strcpy(msg4," Cheat");
strcpy(msg5," Instructions");
strcpy(msg6," Return to Main Menu");
strcpy(msg7," Return to DOS");
strcpy(msg8," Exit Menu <ESC>");
prtfstr(1,2,msg1,w1->wc.text_color,80);
prtfstr(1,4,msg2,w1->wc.text_color,80);
prtfstr(1,6,msg3,w1->wc.text_color,80);
prtfstr(1,8,msg4,w1->wc.text_color,80);
prtfstr(1,10,msg5,w1->wc.text_color,80);
prtfstr(1,12,msg6,w1->wc.text_color,80);
prtfstr(1,14,msg7,w1->wc.text_color,80);
prtfstr(1,16,msg8,w1->wc.text_color,80);
do {
if (UWin || ULose) { /* if game just finished put cursor on Restart */
init_entryno = 1;
UWin = FALSE;
ULose = FALSE;
}
else init_entryno = entryno;
entryno = HiliteSettingsEntry(w1,init_entryno); /* get an entry */
switch(entryno) {
case 1:
RestartFlag = TRUE;
Done = TRUE;
break;
case 2:
SoundToggle = !SoundToggle;
break;
case 3:
HelpOn = !HelpOn;
break;
case 4:
Cheat(mode);
break;
case 5:
Instructions(1);
Instructions(3);
break;
case 6:
RestartFlag = Done = TRUE;
GoForever = Loop = FALSE;
break;
case 7:
Quit();
break;
case 8:
Done = TRUE;
break;
}
} while (!Done);
if (MousePresent) mouse_on(1);
set_mouse_window(1,1,80,25);
move_mouse(mousex,mousey); /* move mouse to its original position */
rmv_win(w1);
}
void GiveWinOrLoseStatus(int msg)
{
char *entries[] = {
" SORRY ... YOU LOST !", /* msg = 0 */
" CONGRATULATIONS ... YOU WON !!!", /* msg = 1 */
" NEXT TIME ... DON'T CHEAT !" /* msg = 2 */
};
windesc *win;
int i,j;
if (MousePresent) mouse_off(1);
win = draw_win(CTRWIN,CTRWIN,35,5,"",popup,&errcolors);
slct_win(win);
prtfstr(1,2,entries[msg],win->wc.text_color,80);
if (MousePresent) mouse_on(1);
while(!kbhit() && !button_press(LEFT) && !button_press(RIGHT));
if (kbhit()) getch();
rmv_win(win);
}
void Cheat(int mode)
{
windesc *cheat_win;
int i,j,count;
if ((mode == 0) || (mode == 1)) count = 4;
else count = 5;
if (MousePresent) mouse_off(1);
HasCheated = TRUE;
cheat_win = draw_win(19,10,38,4,"",popup,&graycolors);
slct_win(cheat_win);
if (mode == 1) { /* Mini Mastermind */
for (i = 0; i <= count-1; i++) DrawPeg(mode, PegAnswer[i], 10*i + 1, 2);
}
else if ((mode == 0) || (mode == 2)) { /* Mastermind or Adv Mastermind */
for (i = 0; i <= count-1; i++) DrawPeg(mode, PegAnswer[i], 8*i + 1, 2);
}
if (MousePresent) mouse_on(1);
while(!kbhit() && !button_press(LEFT) && !button_press(RIGHT));
if (kbhit()) getch();
rmv_win(cheat_win);
}
void Instructions(int select)
/* select works as follows :
1: give windows 1, 2
2: give windows 3, 4, 5 & 6
3: give window 7 */
{
char *instr_entries[] = {
"",
"The object is to guess a secret code.", /* window 1 */
"Use the mouse to select one of the",
"numbered pegs on the left, then place",
"it in a location below. For keyboard,",
"use left & right arrows and enter the",
"appropriate peg number. After filling",
"each of the guess locations, select",
"MAKE GUESS, or hit a carriage return.",
"< press a button or key to continue >",
"After making a guess, you will be", /* window 2 */
"provided with clues. Each black clue",
"indicates one guess is of the correct",
"color & in the correct location. Each",
"white clue indicates one guess is of",
"the correct color but in the wrong",
"location. Use these clues to find the",
"secret code (get all black clues). ",
"< press a button or key to continue >",
"EXAMPLE : Suppose the solution to the", /* window 3 */
"secret code is (3 4 1 4) as shown",
"above. If you make the guess shown",
"below (4 1 1 3) and then select MAKE",
"GUESS, you would be presented with",
"the clues shown at the lower right",
"corner of the screen. ",
" ",
"< press a button or key to continue >",
"The one BLACK clue corresponds to a 1", /* window 4 */
"being an exact match to part of the",
"code. The WHITE clues correspond to a",
"4 and a 3 being correct guesses, but",
"in the incorrect locations. Try to",
"guess the exact solution (get four",
"black clues) in ten or less turns by",
"using information in the clues. ",
"< press a button or key to continue >",
"MINI MASTERMIND : Same as Mastermind", /* window 5 */
"except you get only 6 turns to find",
"the code. This is more difficult. ",
"ADVANCED MASTERMIND : There are five",
"tiles in the code, each may take on",
"either 8, 9, 10 or 11 colors. You get",
"twelve turns to find the secret code.",
"This is much more difficult. ",
"< press a button or key to continue >",
"This concludes the instructions for", /* window 6 */
"MASTERMIND , MINI MASTERMIND and",
"ADVANCED MASTERMIND. These games are",
"easy to play but they are not so easy",
"to master. The various levels of play",
"may challenge you for a long time. ",
" ",
" ",
"< press button or key for main menu >",
"If you need to see the complete set", /* window 7 */
"of instructions, return to the main",
"menu and select 'Instructions for",
"Play'. However, when you do this you",
"will lose any progress you may have",
"made in the present game. ",
" ",
" ",
"< press button or key for main menu >",
};
windesc *instr_win;
int i;
if (MousePresent) mouse_off(1);
instr_win = draw_win(18,CTRWIN,39,11,"INSTRUCTIONS",popup,&defcolors);
slct_win(instr_win);
if (select == 1) {
for (i=1; i<=9; i++)
prtfstr(1,i,instr_entries[i],instr_win->wc.text_color,80);
while ( !mouse_trigger(1) );
for (i=10; i<=18; i++)
prtfstr(1,i-9,instr_entries[i],instr_win->wc.text_color,80);
while ( !mouse_trigger(1) );
}
else if (select == 2) {
for (i=19; i<=27; i++)
prtfstr(1,i-18,instr_entries[i],instr_win->wc.text_color,80);
while ( !mouse_trigger(1) );
for (i=28; i<=36; i++)
prtfstr(1,i-27,instr_entries[i],instr_win->wc.text_color,80);
while ( !mouse_trigger(1) );
for (i=37; i<=45; i++)
prtfstr(1,i-36,instr_entries[i],instr_win->wc.text_color,80);
while ( !mouse_trigger(1) );
for (i=46; i<=54; i++)
prtfstr(1,i-45,instr_entries[i],instr_win->wc.text_color,80);
while ( !mouse_trigger(1) );
}
else if (select == 3) {
for (i=55; i<=63; i++)
prtfstr(1,i-54,instr_entries[i],instr_win->wc.text_color,80);
while ( !mouse_trigger(1) );
}
if (MousePresent) mouse_on(1);
rmv_win(instr_win);
}
void Quit(void)
{
if (MousePresent) mouse_reset();
textmode(initial_info.currmode);
gotoxy(1,1);
TurnBorderOff();
TurnCursorOn();
clrscr();
exit(0);
}
void BeepHi(void)
{
if (SoundToggle)
{
sound(660);
delay(100);
nosound();
delay(100);
}
}
void BeepLo(void)
{
if (SoundToggle)
{
sound(220);
delay(300);
nosound();
delay(100);
}
}
void RunMasterMind(void)
{
int Temp;
do
{
GoForever = FALSE;
UWin = ULose = RestartFlag = HasCheated = FALSE;
PegNumber = 0;
PegPosition = 1;
for (i = 0 ; i <= 4 ; i++) PegGuess[i] = 0;
RandomPattern(mode);
DrawNewIndicator(mode);
if (MousePresent) mouse_on(1); else mouse_off(1);
do
{
if (button_press(LEFT) || kbhit()) {
/* see if keyboard is being used and if key is a functionkey */
if (kbhit()) {
ch = toupper(getch()); MouseValid = FALSE; UseKbd = TRUE;
if (ch == NUL) { /* it must be a function key */
ch = getch();
FunctionKey = TRUE;
}
else FunctionKey = FALSE;
}
else { ch = ' '; MouseValid = TRUE; }
if (!FunctionKey && ((ch >= 48) && (ch <= 98))
|| (MouseValid && mouse_in_box(0,
PegCoords[0],PegCoords[1],
PegCoords[2],PegCoords[3]))) {
Temp = GetPegType(mode, ch);
if (Temp != 0) {
PegNumber = Temp;
if ((ch >= 48)&&(ch <= 98)) PutPegType(mode);
}
}
else if ((FunctionKey && ch == 75) || (FunctionKey && ch == 77))
SetPegPosition(mode);
else if (MouseValid && mouse_in_box(0,
PutCoords[0],PutCoords[1],
PutCoords[2],PutCoords[3]))
if (PegNumber != 0) PutPegType(mode);
else if (HelpOn) {
if (PegNumber == 0) GiveHelpInformation(1);
else if (PegGuess[0] != 0 && PegGuess[1] != 0
&& PegGuess[2] != 0 && PegGuess[3] != 0)
GiveHelpInformation(2);
else GiveHelpInformation(3); }
else BeepHi();
else if (ch == RTN || (MouseValid && mouse_in_box(0,
MakeGuessCoords[0],MakeGuessCoords[1],
MakeGuessCoords[2],MakeGuessCoords[3]))) MakeGuess(mode);
else if ((FunctionKey && ch==F1)||(MouseValid && mouse_in_box(0,
OptionsCoords[0],OptionsCoords[1],
OptionsCoords[2],OptionsCoords[3]))) SettingsMenu();
else if (HelpOn) {
if (PegNumber == 0) GiveHelpInformation(1);
else if (PegGuess[0] != 0 && PegGuess[1] != 0
&& PegGuess[2] != 0 && PegGuess[3] != 0)
GiveHelpInformation(2);
else GiveHelpInformation(3);}
}
else if (button_press(RIGHT))
if (mouse_in_box(0, MakeGuessCoords[0],MakeGuessCoords[1],
MakeGuessCoords[2],MakeGuessCoords[3])) MakeGuess(mode);
else SettingsMenu();
} while (!RestartFlag);
} while (GoForever);
}
void main(void)
{
InitScreenSettings();
init_win();
SoundToggle = TRUE;
Forever = TRUE;
InitializeMouse();
ShareWareScreen();
do {
ShowMainMenu = TRUE;
Loop = TRUE;
while (ShowMainMenu) PrintDirections();
NumPegs = NumberOfPegColors(mode);
do {
Init();
RunMasterMind();
} while (Loop);
} while (Forever);
TurnBorderOff();
TurnCursorOn();
}